import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/', name: 'main', redirect: "/admin/message", component: () => import('../views/Main.vue'),
    children: [
      { path: 'admin/edit', name: 'edit', component: () => import('../views/admin/Edit.vue') },
      { path: 'admin/list', name: 'list', component: () => import('../views/admin/List.vue') },
      { path: 'admin/message/:id?', name: 'message',component: () => import('../views/admin/Message.vue') },
    ]
  },
  {
    path: "/login", name: "login", meta: { isPublic: true }, component: () => import("../views/login/Login.vue")
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  // console.log(to.meta);
  if (!to.meta.isPublic && !localStorage.token) {
    console.log("请先登录!");
    return router.push("/login")
  }
  next()
})

export default router
